package com.kye.express.business.jd_reservation.list;


import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.RefreshListView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.RequestPage;
import com.kye.model.bean.jd_reservation.JDReservationBean;
import com.kye.model.bean.user.User;
import com.kye.model.service.JDService;

import io.reactivex.SingleSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;

public interface JdReservationListContract {
    interface JdReservationListView extends IView , RefreshListView<JDReservationBean> {
    }

    class JdReservationListPresenter extends BasePresenter<JdReservationListView> {
        public void load(int pageIndex, int pageSize, boolean hasData) {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .flatMap(new Function<User, SingleSource<OpenApiResult<JDReservationBean>>>() {
                        @Override
                        public SingleSource<OpenApiResult<JDReservationBean>> apply(User user) throws Exception {
                            return RetrofitManager.getInstance()
                                    .createOpenApiService(JDService.class)
                                    .getJDList(new RequestPage(user.getPhone(), pageIndex, pageSize));
                        }
                    })
                    .compose(getView().bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<JDReservationBean>() {
                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading(pageIndex, hasData);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<JDReservationBean> result, JDReservationBean jdReservationBean) {
                            getView().onLoadData(jdReservationBean, pageIndex, hasData);
                        }

                        @Override
                        public void onApiError(OpenApiResult<JDReservationBean> result, int code, String msg) {
                            getView().onApiError(pageIndex, hasData, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError(pageIndex, hasData);
                        }

                        @Override
                       public void onLoaded() {
                            getView().onLoaded(pageIndex, hasData);
                        }
                    });
        }
    }
}
